package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.permission.PermissionPageVo;
import com.wanxi.education.model.vo.user.PermissionVO;
import com.wanxi.education.service.PermissionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: Kang
 * @Date: 2021/9/28 14:07
 * @Description: 角色
 * @Version: 1.0
 */
@Api(tags={"权限相应API"})
@RestController
@RequestMapping("/back/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value="分页查询所有权限")
    @ApiParam(name = "permissionPageVo", value = "分页参数", required = true)
    @PostMapping("/findPage")
    public CommonResult findPage(@RequestBody PermissionPageVo permissionPageVo){
        List<PermissionVO> permissionVOVOList = permissionService.findPage(permissionPageVo.getPageNum(),permissionPageVo.getPageSize());
        PageInfo<PermissionVO> permissionVOPageInfo = new PageInfo<>(permissionVOVOList);
        return CommonResult.success(permissionVOPageInfo);
    }

    @PostMapping("/add")
    @ApiOperation(value="新增权限", notes="添加权限,权限页面添加弹窗")
    @ApiParam(value = "新增权限参数", required = true)
    public CommonResult add(@RequestBody PermissionVO permissionVO){
        Boolean b = permissionService.add(permissionVO);
        return CommonResult.success(b);
    }


    @GetMapping("/find-by-permission-id")
    @ApiParam(name = "permissionId",value = "权限ID")
    @ApiOperation(value="通过权限Id查询权限信息")
    public CommonResult findByPermissionId(@RequestParam(required = true) Long permissionId){
        if (permissionId == null) {
            CommonResult.validateFailed("参数不能为空");
        }
        PermissionVO permissionVO = permissionService.findByPermissionId(permissionId);
        return CommonResult.success(permissionVO);
    }

    @PostMapping("/update-by-id")
    @ApiOperation(value="超级管理修改权限")
    public CommonResult updateById(@ApiParam(value = "修改后的信息", required = true) @RequestBody PermissionVO permissionVO){
        Boolean b = permissionService.updateById(permissionVO);
        return CommonResult.success(b);
    }

    @GetMapping("/delete-by-permission-id")
    @ApiParam(name = "roleId",value = "权限ID")
    @ApiOperation(value="通过权限ID删除权限信息")
    public CommonResult deleteByPermissionId(@RequestParam(required = true) Long permissionId){
        if (permissionId == null) {
            CommonResult.validateFailed("参数不能为空");
        }
        Boolean b = permissionService.deleteByPermissionId(permissionId);
        return CommonResult.success(b);
    }


    @ApiOperation("通过用户ID查询所属角色")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "roleId",value = "需要查询的角色ID",dataType = "Long",paramType = "query")
    )
    @GetMapping("/find-by-role-id")
    public CommonResult findByRoleId(Long roleId) {
        Set<Long> roldIds = new HashSet<>();
        roldIds.add(roleId);
        Set<PermissionVO> roleSet = permissionService.findByRoleId(roldIds);
        return CommonResult.success(roleSet);
    }

    @ApiOperation("查询所有的权限,不分页")
    @GetMapping("/findAll")
    public CommonResult<List<PermissionVO>> findAll() {
        List<PermissionVO> list = permissionService.findAll();
        return CommonResult.success(list);
    }



}
